package com.dingding.order.core.history.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.dto.AbstractResponse;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.order.code.order.PublishCarType;
import com.dingding.order.core.history.dao.IHistoryOrderDao;
import com.dingding.order.core.history.dto.TakeMileageDTO;
import com.dingding.order.core.history.service.IHistoryOrderService;
import com.dingding.order.dto.history.HistoryOrderDto;
import com.dingding.order.dto.history.ReqCompleteAndCancelOrder;
import com.dingding.order.dto.history.ResCompleteAndCancelOrder;
import com.dingding.order.dto.history.ResHistoryOrder;
import com.dingding.order.processer.ProcesserFactory;


/**
 * 
 * @ClassName: DriverMergeOrderServiceImpl 
 * @Description: 专车司机获取历史订单
 * @author YHQ
 * @date 2016年11月2日 下午4:31:56
 * @since V2.5.5
 * 
 */
@Component("driverMergeOrderService")
public class DriverMergeOrderServiceImpl extends AbstractResponse<ResCompleteAndCancelOrder, ReqCompleteAndCancelOrder> implements IHistoryOrderService {

	@Resource(name = "processerFactory")
	private ProcesserFactory processerFactory;
	
	@Resource(name = "specialHistoryOrderDao")
	private IHistoryOrderDao historyOrderDao;
	
	@Override
	public String getHistoryOrders(String clientData) {

		ResponseEntity<ResHistoryOrder> responseEntity = new ResponseEntity<ResHistoryOrder>();
		ReqCompleteAndCancelOrder reqCompleteAndCancelOrder = super.parseRequestJson(clientData);
		
		if(!super.validateParam(responseEntity, reqCompleteAndCancelOrder)){
			return super.responseBeanToJson(responseEntity);
		}
		
		List<HistoryOrderDto> historyOrderDtos = processerFactory.getOrderProcesser(PublishCarType.SPECIAL)
				.completeAndCancelOrder(reqCompleteAndCancelOrder.getUserId(), reqCompleteAndCancelOrder.getPageIndex(), reqCompleteAndCancelOrder.getPageSize(),reqCompleteAndCancelOrder.getClientType());
		
		// 获取接乘里程
		TakeMileageDTO takeMileageDTO = historyOrderDao.takeMileage(reqCompleteAndCancelOrder.getUserId());
		
		ResHistoryOrder resHistoryOrder = new ResHistoryOrder();
		resHistoryOrder.setOrderList(historyOrderDtos);
		
		// 设置接乘里程
		if(takeMileageDTO != null && takeMileageDTO.getPickUpMeters() != null){
			resHistoryOrder.setPickUpMeters(takeMileageDTO.getPickUpMeters());
		}else{
			resHistoryOrder.setPickUpMeters(new BigDecimal(0));
		}
		
		responseEntity.setCode(HttpStatus.SC_OK);
		responseEntity.setPromptMessage("请求成功");
		responseEntity.setResponseBody(resHistoryOrder);
		
		return super.responseBeanToJson(responseEntity);
	}

	@Override
	public String getHistoryOrdersByCarType(String clientData, PublishCarType carType) {
		return null;
	}

}
